package com.mcintyret.site.web.controller.users.registration;

import com.mcintyret.site.service.users.registration.RegistrationManager;
import com.mcintyret.site.web.controller.users.forms.RegistrationForm;
import com.mcintyret.site.web.controller.BaseController;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * User: mcintyret2
 * Date: 06/04/2012
 */

@Controller
@RequestMapping("/register")
public class RegistrationController extends BaseController {

    @Resource
    private RegistrationManager registrationManager;

    @RequestMapping(method = RequestMethod.POST)
    public String registerNewUser(
            @Valid RegistrationForm registrationForm,
            BindingResult bindingResult) {

        boolean success = registrationManager.registerAndLoginNewUser(registrationForm, bindingResult);

        return success ? "redirect:/home" : null;
    }

    @RequestMapping(method = RequestMethod.GET)
    public RegistrationForm registrationPage() {
        return new RegistrationForm();
    }

}
